package com.aos.erp.schools.service.car.impl;

import com.aos.erp.common.dao.mapper.Condition;
import com.aos.erp.common.exception.BizException;
import com.aos.erp.schools.enums.student.IsDel;
import com.aos.erp.schools.mapper.car.CarMapper;
import com.aos.erp.schools.mapper.car.CarPictureMapper;
import com.aos.erp.schools.model.car.Car;
import com.aos.erp.schools.model.car.CarPicture;
import com.aos.erp.schools.model.car.CarVo.CarVo;
import com.aos.erp.schools.model.car.Dto.CarDto;
import com.aos.erp.schools.model.sys.Department;
import com.aos.erp.schools.model.sys.User;
import com.aos.erp.schools.service.car.CarPictureService;
import com.aos.erp.schools.service.car.CarService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {
    @Resource
    private CarPictureMapper carPictureMapper;
    @Resource
    private CarMapper carMapper;
    @Resource
    private CarPictureService carPictureService;

    @Override
    public Car getById(long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 新增
     * @param carDto
     * @throws BizException
     */
    @Override
    public void addBean(CarDto carDto) throws BizException {
        Car car = new Car();
        Date date = new Date();
        BeanUtils.copyProperties(carDto, car);
        car.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
        car.setCreated(date);
        car.setUpdated(date);
        car.setDriversSchoolId(Long.valueOf(carDto.getDriversSchoolId()));
        baseMapper.saveObject(car);

        Long carId = car.getId();
        if (StringUtils.isNotEmpty(carDto.getCarTravelCard())) {
            CarPicture carPicture = new CarPicture();
            carPicture.setCarId(carId);
            carPicture.setCreated(date);
            carPicture.setUpdated(date);
            carPicture.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
            carPicture.setFileId(Long.valueOf(carDto.getCarTravelCard()));
            carPicture.setType(1);
            carPictureMapper.save(carPicture);
        }

        if (StringUtils.isNotEmpty(carDto.getCarPicIds())) {
            List<CarPicture> carPictures = new ArrayList<>();
            String carPicIds = carDto.getCarPicIds().substring(0, carDto.getCarPicIds().length() - 1);
            String[] imgs = carPicIds.split(",");
            CarPicture carPicture = new CarPicture();
            carPicture.setCarId(carId);
            carPicture.setCreated(date);
            carPicture.setUpdated(date);
            carPicture.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
            carPicture.setType(2);
            for (String img : imgs) {
                carPicture.setFileId(Long.valueOf(img));
                carPictures.add(carPicture);
            }
            carPictureMapper.insertBatch(carPictures);
        }
    }

    /**
     *修改车辆
     * @param carDto
     * @return
     */
    public void updateObject(CarDto carDto)  {
        if(carDto.getId()==null){
            //throw new BizException("请传入ID");
        }
        Car car = new Car();
        Date date = new Date();
        BeanUtils.copyProperties(carDto, car);
        car.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
        car.setUpdated(date);
        car.setDriversSchoolId(Long.valueOf(carDto.getDriversSchoolId()));
        baseMapper.updateObject(car);

        Long carId = car.getId();
        if (StringUtils.isNotEmpty(carDto.getCarTravelCard())) {
            Condition condition = new Condition(CarPicture.class);
            condition.eqColumn("file_id", carDto.getCarTravelCard());
            CarPicture carPicture = carPictureMapper.selectOne(condition.getWrapper());
            if (carPicture == null) {
                carPicture = new CarPicture();
                carPicture.setCarId(carId);
                carPicture.setCreated(date);
                carPicture.setUpdated(date);
                carPicture.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
                carPicture.setFileId(Long.valueOf(carDto.getCarTravelCard()));
                carPicture.setType(1);
                carPictureMapper.save(carPicture);
            }
        }

        if (StringUtils.isNotEmpty(carDto.getCarPicIds())) {
            List<CarPicture> carPictures = new ArrayList<>();
            String carPicIds = carDto.getCarPicIds().substring(0, carDto.getCarPicIds().length() - 1);
            String[] imgs = carPicIds.split(",");
            for (String img : imgs) {
                Condition condition = new Condition(CarPicture.class);
                condition.eqColumn("file_id", img);
                CarPicture carPicture = carPictureMapper.selectOne(condition.getWrapper());
                if(carPicture==null){
                    carPicture = new CarPicture();
                    carPicture.setCarId(carId);
                    carPicture.setCreated(date);
                    carPicture.setUpdated(date);
                    carPicture.setIsDel(Integer.valueOf(IsDel.normal.getValue().toString()));
                    carPicture.setType(2);
                    carPicture.setFileId(Long.valueOf(img));
                    carPictures.add(carPicture);
                }
            }
            carPictureMapper.insertBatch(carPictures);
        }
    }

    @Override
    public IPage<CarVo> queryByPage(Integer pageIndex, Integer pageSize, String carNumbers, String registerTime, String schoolDepCode) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotEmpty(registerTime)) {
            String[] times = registerTime.split(" 到 ");
            startTime = times[0];
            endTime = times[1];
        }
        Page<CarVo> pageBean = new Page<CarVo>(pageIndex, pageSize);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("carNumbers", carNumbers);
        map.put("startTime", startTime);
        map.put("endTime", endTime);
        map.put("isDel", IsDel.normal.getValue());
        map.put("depCode", schoolDepCode);
        map.put("pageBean", pageBean);
        List<CarVo> list = baseMapper.queryList(map);
        IPage<CarVo> page = pageBean;
        page.setRecords(list);
        return page;
    }

    /**
     * 删除车辆
     *
     * @param id
     * @return
     */
    public int isDel(String id) throws Exception {
        try {
            Car car = new Car();
            car.setId(Long.valueOf(id));
            car.setIsDel(Integer.valueOf(IsDel.del.getValue().toString()));
            car.setUpdated(new Date());
            int i = baseMapper.updateObject(car);
            return i;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }


    /**
     * 获取车辆信息
     * @param id  车辆id
     * @return
     */
    public CarVo getInfo(String id) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", id);
        List<CarVo> list = baseMapper.queryList(map);
        if (list != null && list.size() > 0) {
            CarVo carVo = list.get(0);
            List<Map<String, Object>> carTravelCard = carPictureService.getInfo(carVo.getId().toString(), "1");
            List<Map<String, Object>> carPicIds = carPictureService.getInfo(carVo.getId().toString(), "2");
            if(carTravelCard!=null && carTravelCard.size()>0){
                carVo.setCarTravelCard(carTravelCard.get(0));
            }
            carVo.setCarPicIds(carPicIds);
            return carVo;
        }
       return null;
    }
}